//
//  LoanScreenView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol LoanScreenViewDelegate {
    @objc optional func selectLabelType(currentView:LoanScreenView) //选择标签
}

class LoanScreenView: UIView {
    
    weak var delegate : LoanScreenViewDelegate?
    
    fileprivate let titleLabel = UILabel() //标题
    let textView = UIButton() //点击按钮
    fileprivate let textLabel = UITextField() //内容
    fileprivate let imageView = UIImageView() //图片
    
    var showSelect = false //是否显示选择框
    
    var title : String! {//标题
        didSet{
            titleLabel.text = title
        }
    }
    
    var placeholder : String! { //提示内容
        didSet{
            textLabel.placeholder = placeholder
        }
    }
    
    var text : String! {//内容
        didSet{
            textLabel.text = text
        }
    }

    init(textWidth:CGFloat?=ScreenW/3*2) {
        super.init(frame: .zero)
        
        titleLabel.numberOfLines = 0
        titleLabel.textAlignment = .right
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(titleLabel)
        
        textView.layer.borderColor = UIColor.black.cgColor
        textView.layer.borderWidth = 1
        textView.addTarget(self, action: #selector(selectLabelType), for: .touchUpInside)
        self.addSubview(textView)
        
        textLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        textLabel.isEnabled = false
        textLabel.delegate = self
        textView.addSubview(textLabel)
        
        imageView.image = UIImage.init(named: "bottom_arrow")
        textView.addSubview(imageView)
        
        textView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.centerY.equalToSuperview()
            make.width.equalTo(textWidth!)
            make.height.equalTo(35)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.right.equalTo(textView.snp.left).offset(RATIO(maxNum: -5))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            
        }
        
        imageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -5))
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.centerY.height.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 5))
            make.right.equalTo(imageView.snp.left).offset(RATIO(maxNum: -5))
        }
    }
    
    //MARK: 选择标签
    @objc fileprivate func selectLabelType() {
    
        self.delegate?.selectLabelType!(currentView: self)
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LoanScreenView : UITextFieldDelegate {
    
    //MARK: 不可编辑
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        
        return false
    }
}
